package com.googlecode.chigs.web.config.configpair;

import com.googlecode.chigs.domain.ConfigPair;
import com.googlecode.chigs.domain.ConfigRevision;
import com.googlecode.chigs.domain.ConfigValue;
import com.googlecode.chigs.domain.Revision;
import com.googlecode.chigs.repository.ChigsFacade;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.ArrayList;
import java.util.List;

public class EditOrCreateConfigPairStep extends WizardStep {
	@SpringBean
	private ChigsFacade chigs;

	public EditOrCreateConfigPairStep(final ConfigRevision configRevision, final boolean editable) {
		for (Revision revision : configRevision.getRevisions()) {
			revision.setConfigPairs(chigs.loadConfigPairsByRevision(revision));
		}
		
		final List<ConfigValue> loadConfigValuesByConfigRevision = new ArrayList<ConfigValue>();
		if (!configRevision.isNew()) {
			loadConfigValuesByConfigRevision.addAll(chigs.loadConfigValuesByConfigRevision(configRevision));
			configRevision.setConfigValues(new ArrayList<ConfigValue>());
		}

		add(new PropertyListView<Revision>("module", new PropertyModel<List<Revision>>(configRevision, "revisions")) {
			@Override
			protected void populateItem(ListItem<Revision> item) {
				item.add(new Label("module.name"));
				item.add(new Label("nr"));
				item.add(new PropertyListView<ConfigPair>("configpair", new PropertyModel<List<ConfigPair>>(item.getModelObject(), "configPairs")) {
					@SuppressWarnings("unchecked")
					@Override
					protected void populateItem(ListItem<ConfigPair> item) {
						String cssClass;
						if (item.getIndex() % 2 != 0) {
							cssClass = "odd";
						} else {
							cssClass = "even";
						}
						item.add(new AttributeModifier("class", true, new Model<String>(cssClass)));
						
						final Label label = new Label("name");
						item.add(label);
						item.add(new Label("defaultData"));
						item.add(new Label("type"));

						ConfigValue configValue = new ConfigValue();
						for (ConfigValue value : loadConfigValuesByConfigRevision) {
							if (value.getConfigPair().getId().longValue() == item.getModelObject().getId().longValue()) {
								configValue.setData(value.getData());
								break;
							}
						}
						configValue.setConfigPair(item.getModelObject());
						configValue.setConfigRevision(configRevision);
						configRevision.getConfigValues().add(configValue);
						
						item.add(new TextField<String>("value", new PropertyModel<String>(configValue, "data"))
								.setRequired(item.getModelObject().isMandatory())
								.setLabel((IModel<String>) label.getDefaultModel())
								.setEnabled(editable));
					}
				});
			}
		});
	}
}
